﻿using Grpc.Core;
using Grpc.Core.Interceptors;
using MX.Core.Log;
using MX.Core.ORM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UserCenter.Api.Core;
using UserCenter.Api.Interceptors;
using UserCenter.Api.Services;
using UserCenter.BLL;
using UserCenter.Grpc.Sdk;

namespace UserCenter.Api.Serevers
{
    public class ConsoleApiServer : IServer
    {
        ILogger logger = Logger.GetLogger("SdkApi");
        public void Run()
        {
            try
            {
                const int port = 8007;
                DatabaseConfig.RegisterStringKey(Database.MemberCenter, "MemberCenter");
                //var keyCertPair = new KeyCertificatePair("data/server1.pem", "data/server1.key"); 
                //var serverCredentials = new SslServerCredentials(new[] { keyCertPair });
                var serverCredentials = ServerCredentials.Insecure;
                ServerServiceDefinition serviceDefinition = ConsoleApi.BindService(new ConsoleApiImpl( ));
                serviceDefinition = serviceDefinition.Intercept(new ConsoleApiInterceptor());
                Server server = new Server
                {
                    Services = { serviceDefinition },
                    Ports = { new ServerPort("0.0.0.0", port, serverCredentials) }
                };
                server.Start();
                logger.Debug($"已在端口[{port}]启动服务{nameof(ConsoleApiServer)}");
            }
            catch (Exception ex)
            {
                logger.Error(ex, "");
            }

        }
    }
}
